package com.thinkgem.jeesite.modules.lottery.web; /**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.MapListUtils;
import com.thinkgem.jeesite.modules.lottery.entity.*;
import com.thinkgem.jeesite.modules.lottery.service.ActivityService;
import com.thinkgem.jeesite.modules.lottery.service.PrizeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抽奖活动管理Controller
 * @author muser
 * @version 2019-07-29
 */
@Controller
@RequestMapping(value = "${adminPath}/lottery/activity")
public class ActivityController extends BaseController {

	@Autowired
	private ActivityService activityService;
	@Autowired
	private PrizeService prizeService;
	@Autowired
	private MapListUtils mapListUtils;
	@ModelAttribute
	public Activity get(@RequestParam(required=false) String id) {
		Activity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = activityService.get(id);
		}
		if (entity == null){
			entity = new Activity();
		}
		return entity;
	}

	@RequiresPermissions("lottery:activity:view")
	@RequestMapping(value = {"list", ""})
	public String list(Activity activity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Activity> page = activityService.findPage(new Page<Activity>(request, response), activity);
		model.addAttribute("page", page);
		model.addAttribute("statusList", mapListUtils.getStatus());
		model.addAttribute("formList", mapListUtils.getForms());
		model.addAttribute("mangerList",mapListUtils.getMangers());
		Map<String,String> statusMap = new HashMap();
		for(ActivityStatus s: mapListUtils.getStatus()){
			statusMap.put(s.getId(),s.getDescription());
		}
		Map<String,String> formMap = new HashMap();
		for(ActivityForm f: mapListUtils.getForms()){
			formMap.put(f.getId(),f.getDescription());
		}
		Map<String,List<Prize>> prizeMap=new HashMap<>();
		for(Activity a:page.getList()){
			prizeMap.put(a.getId(), mapListUtils.getPrizes(a.getId()));
		}
		Map<String,String> wechatUserMap=new HashMap<>();
		for (WechatUser wechatUser: mapListUtils.getMangers()){
			wechatUserMap.put(wechatUser.getId(),wechatUser.getNickname());
		}
		model.addAttribute("statusMap",statusMap);
		model.addAttribute("formMap",formMap);
		model.addAttribute("prizeMap",prizeMap);
		model.addAttribute("wechatUserMap",wechatUserMap);
		System.out.println(wechatUserMap.toString());
		return "modules/lottery/activityList";
	}

	@RequiresPermissions("lottery:activity:view")
	@RequestMapping(value = "form")
	public String form(Activity activity, Model model) {
		model.addAttribute("activity", activity);
		model.addAttribute("statusList", mapListUtils.getStatus());
		model.addAttribute("formList", mapListUtils.getForms());
		model.addAttribute("managerList", mapListUtils.getMangers());
		if (activity==null){
			List<Prize> nullList=new ArrayList<>();
			model.addAttribute("prizeList",nullList);
		}
		else {
			model.addAttribute("prizeList", mapListUtils.getPrizes(activity.getId()));
		}
		return "modules/lottery/activityForm";
	}

	@RequiresPermissions("lottery:activity:edit")
	@RequestMapping(value = "save")
	public String save(Activity activity, String[] prize_id, String[] prize_description, String[] prize_name, Integer[] prize_quantity, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, activity)){
			return form(activity, model);
		}
		activityService.save(activity);
		System.out.println("[一个活动]"+activity.toString());
		Activity activity1 = activityService.findList(activity).get(0);
		Prize prize1 = new Prize();
		prize1.setActivityId(activity1.getId());
		prizeService.delete(prize1);
		System.out.println(activity1.getId());
		for (int i=0;i<prize_description.length;i++){
			Prize prize = new Prize();
			prize.setActivityId(activity1.getId());
			prize.setDescription(prize_description[i]);
			prize.setName(prize_name[i]);
			prize.setQuantity(prize_quantity[i]);
			prize.setSort(i);
			prizeService.save(prize);
			System.out.println("[一个奖品]"+prize.toString());
		}
		addMessage(redirectAttributes, "保存抽奖活动成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/activity/?repage";
	}

	@RequiresPermissions("lottery:activity:edit")
	@RequestMapping(value = "delete")
	public String delete(Activity activity, RedirectAttributes redirectAttributes) {
		activityService.delete(activity);
		Prize prize=new Prize();
		prize.setActivityId(activity.getId());
		prizeService.delete(prize);
		addMessage(redirectAttributes, "删除抽奖活动成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/activity/?repage";
	}
}